package com.ftg.learn.chapter03.business.service;

import com.ftg.learn.chapter03.business.entity.UserEntity;
import com.ftg.learn.chapter03.business.util.Info;

/**
 * 用户业务逻辑处理类
 *
 * @author 布衣
 */
public class UserService {

    UserEntity userEntity = new UserEntity();

    public boolean register(String username,String password,String confirmPassword){

        if(username == null || username.isEmpty()){
            System.out.println("对不起，用户名不能为空");
            return false;
        }

        if(password == null || password.isEmpty()){
            System.out.println("对不起，密码不能为空");
            return false;
        }

        if (!password.equals(confirmPassword)) {
            System.out.println("两次输入的密码不相同，不能注册");
            return false;
        }
        userEntity.addUser(username,password);
        return true;
    }

    public Info login(String loginUser, String loginPass, String capath) {

        if(loginUser == null || loginUser.isEmpty()){
            return new Info(500,"请输入您的用户名");
        }

        if(capath == null || capath.isEmpty()){
            return new Info(500,"验证码不能为空");
        }

        if(loginPass == null || loginPass.isEmpty()){
            return new Info(500,"请输入您的密码");
        }


        String[] users = userEntity.getUsers();
        for (int i = 0; i < users.length; i+=2) {
            if(users[i] != null && users[i].equals(loginUser) && users[i+1].equals(loginPass)){
                return new Info(200,"恭喜您登陆成功!!");
            }
        }
        return new Info(500,"对不起，用户名或者密码不存在！！！");
    }
}
